package gameDisplayer;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import gameManager.GameManager;
import gameManager.Interactable;

public class GameDisplayer {

    private Game myGame;
    private JFrame myFrame;
    private GameManager myGameManager;
    private String myLevel;
    private List<Interactable> myInteractableCharacters;
    private Canvas myCanvas;
    
    private  Dimension SIZE;

    public GameDisplayer (GameManager m) {
        myGameManager = m;
        myFrame = new JFrame();
        myInteractableCharacters=m.getInteractableObjects();
        myGame = new Game(myInteractableCharacters,m.getBackgroundImage());
        SIZE = new Dimension(m.getBackgroundImage().getWidth(null), m.getBackgroundImage().getHeight(null));
        initializeFrame();
    }
    
    /**
     * Initializes the Jframe
     */
    public void initializeFrame () {
        myFrame.setSize(SIZE);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setMinimumSize(SIZE);
        myFrame.setLocationRelativeTo(null);
        JMenuBar menuBar = new JMenuBar();
        myFrame.setJMenuBar(menuBar);
        myFrame.setVisible(true);
    }
    
    public void paintGameObjects(){
        Graphics pen=myCanvas.getGraphics();
        myGame.paintComponent((Graphics2D)pen, SIZE);
        
    }
    
    
    
    
    public JComponent getGame (Dimension dim) {
        Canvas canvas = new Canvas(dim, myGame);
        return canvas;
    }

    public void updateDisplay () {
        JComponent canvas = getGame(SIZE);
        myFrame.getContentPane().removeAll();
        myFrame.getContentPane().add(canvas);
        myFrame.repaint();
        myFrame.setTitle("TheGame");
        myFrame.setVisible(true);
    }
    
    private JMenu createLevelMenu () {
        JMenu file = new JMenu("Levels");
        return file;
    }
    
    public void displayStartupMenu() {
        List<String> levelList = myGameManager.getLevels();
        int numberOfLevels = levelList.size();
        myLevel = JOptionPane.showInputDialog(myFrame, "Enter Level from 1 - " + numberOfLevels + ": ");
        myGameManager.setLevel(myLevel);
    }
    public void drawBackground(Image myImage){
        Graphics2D pen=(Graphics2D)myCanvas.getGraphics();
        myGame.drawBackground(pen,myImage);
    }
}
